import { defineStore } from "pinia"
import { baseApi } from "@/network"

const userStore = defineStore({
  id: "userStore",
  state: () => ({
    isLogin: false,
    accountName: "",
    password: "",
  }),
  getters: {
    isLogined(): Boolean {
      let val = localStorage.getItem("IS_LOGIN")
      this.isLogin = Boolean(val)
      return this.isLogin
    },
  },
  actions: {
    goToLogin(data: object): Promise<boolean> {
      
      return baseApi.postJSON('/account/login', data).then((response: any) => {
        if (response.data.code === 0) {
          localStorage.setItem("IS_LOGIN", "true")
          this.isLogin = true
          return Promise.resolve(true)
        }
        return Promise.resolve(false)
      })
    },
  },
})

export default userStore
